//
//  RecommendCollectionViewCell.swift
//  XMYouPin
//
//  Created by 王宗成 on 2019/7/6.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit

class RecommendCollectionViewCell: UICollectionViewCell {
    
    var model:ItemModel? = nil
    {
        didSet
        {
            guard let item = model else {
                
                return
            }
            
            self.contentView.backgroundColor = UIColor.hexColor(hexadecimal: item.bg_color ?? "#000000")
            self.imageView.kf.setImage(with: URL(string:item.pic_url ?? ""))
            self.titleLabel.text = item.title ?? ""
            
            self.subtitleLabel.text = item.subtitle ?? ""
            
            self.titleLabel.textColor = UIColor.hexColor(hexadecimal: item.title_color ?? "#000000")
            self.subtitleLabel.textColor = UIColor.hexColor(hexadecimal: item.subtitle_color ?? "#000000")
        }
    }
    
    
    private lazy var imageView : UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = UIView.ContentMode.scaleAspectFill
        imageView.clipsToBounds = true
        
        self.contentView.addSubview(imageView)
        return imageView
    }()
    
    private lazy var titleLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = UIColor.hexColor(hexadecimal: "#333333")
        label.textAlignment = NSTextAlignment.center
        self.addSubview(label)
        return label
    }()
    
    private lazy var subtitleLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.hexColor(hexadecimal: "#999999")
        label.textAlignment = NSTextAlignment.center
        self.addSubview(label)
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.right.lessThanOrEqualTo(-10)
            make.top.equalTo(5)
            make.height.equalTo(20)
        }
        
        self.subtitleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.right.lessThanOrEqualTo(-10)
            make.top.equalTo(self.titleLabel.snp_bottom)
            make.height.equalTo(15)
        }
        let rate:CGFloat = 420.0/280.0
        self.imageView.snp.makeConstraints { (make) in
            make.top.equalTo(self.subtitleLabel.snp_bottom).offset(5)
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: (130*kHeightRatio - 50)*rate, height: 130*kHeightRatio - 50))
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
